package controller;



import model.Vocabulary;

import org.zkforge.ckez.CKeditor;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Textbox;

import admin.dao.VocabDAO;

public class EditVocabCtrl extends SelectorComposer<Component>{
	private static final long serialVersionUID = 1L;
	
	private Vocabulary currentVocab;

	@Wire
    private Textbox txtName;
	
	@Wire
    private Textbox txtTitle;
	
	@Wire
    private CKeditor txtContent;
	
	
	@Listen("onClick = #btSubmit")
    public void submit(){
		currentVocab.setTitle(txtTitle.getValue());
		currentVocab.setName(txtName.getValue());
		currentVocab.setContent(txtContent.getValue().toString());
		
		VocabDAO.updateVocab(currentVocab);
		
		Sessions.getCurrent().removeAttribute("currentVocab");
		Executions.getCurrent().sendRedirect("vocabularies_layout.jsp");
    }
	
	@Override
	public void doFinally() throws Exception {
		//get Grammar
		this.currentVocab = (Vocabulary) Sessions.getCurrent().getAttribute("currentVocab");
		
		
		txtContent.setValue(currentVocab.getContent());
		txtName.setValue(currentVocab.getName());
		txtTitle.setValue(currentVocab.getTitle());
	}

}
